package com.jy.modules.cims.component.tbj;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.common.sysUtils.ExceptionUtils;
import com.jy.modules.cims.component.api.IJieYueSend;
import com.jy.modules.cims.data.dto.inter.tbj.TBJMessage;
import com.jy.modules.cims.data.dto.inter.tbj.TBJResponse;
import com.jy.modules.cims.data.dto.inter.tbj.jj.AbortDTO;
import com.jy.modules.cims.data.interact.api.JieYueMessage;
import com.jy.modules.cims.data.interact.api.JieYueResponseData;
import com.jy.modules.cims.data.interact.api.request.RequestLoanAbortDTO;
import com.tongbanjie.open.api.enums.EntryResponseEnum;
/**
 * 铜板街实时接口发送，组件类
 * ClassName: TBJSendService 
 * @Description: TODO
 * @author dell
 * @date 2016-6-29
 */
@Service("com.jy.modules.cims.component.tbj.TBJSendService")
public class TBJSendService {


	private static Logger logger = LoggerFactory.getLogger(TBJSendService.class);

	@Autowired
	@Qualifier("com.jy.modules.cims.component.tbj.TBJBusinessClient")
	private ITBJBusinessClient iTBJBusinessClient;
	
	@Autowired
	@Qualifier("jieYueSendService")
	private IJieYueSend jieYueSend;
	/**
	 * 
	 * @Description 铜板街流标接口
	 * @param loanNumber 合同号
	 * @return
	 * @throws Exception   
	 * boolean  
	 * @throws
	 * @author zhangmi
	 * @date 2016-6-29
	 */
	public void sendAbortByLoanNumber(String loanNumber) throws Exception {
		// 调用铜板街接口
		logger.info("铜板街流标接口开始>>");
		AbortDTO abortDto = new AbortDTO();
		abortDto.setExitTime(DateTimeUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
		abortDto.setLoanNumber(loanNumber);
		try {
			TBJResponse tbjResponse = iTBJBusinessClient.abort(abortDto);
			//请求成功
			if(EntryResponseEnum.SUCESS21001.getCode().equals(tbjResponse.getRetCode())){
				String res = ((TBJMessage) tbjResponse.getData()).getResult();
				if (!"2".equals(res)) {
					logger.info("铜板街流标接口调用成功！合同号：" + loanNumber);
				} else {
					logger.info("铜板街流标接口调用失败！合同号：" + loanNumber);
					throw new Exception("铜板街流标接口异常，错误编码：" + ((TBJMessage) tbjResponse.getData()).getResult() + ",错误信息：" + ((TBJMessage) tbjResponse.getData()).getMessage());
				}
			}else{
				logger.info("铜板街流标接口调用失败！合同号：" + loanNumber);
				throw new Exception("铜板街流标接口调用失败！"+tbjResponse.getMessage());
			}
		} catch (Exception e) {
			logger.info("铜板街流标接口调用失败！合同号：" +ExceptionUtils.getStackTraceAsString(e));
			throw new Exception("铜板街流标接口调用失败！"+ExceptionUtils.getStackTraceAsString(e));
		}
		logger.info("铜板街流标接口结束>>");
		
	}
	
	/**
	 * 进件终止接口
	 * @author zhangmi
	 * @date  2016年11月21日
	 * @param cooperorgCode
	 * @param loanNumber
	 * @throws Exception
	 */
	public void sendJieYueAbortLoan(String cooperorgCode,String loanNumber)throws Exception{
		
		logger.info("合作机构【{}】！紧急扣回开始>>",cooperorgCode);
		RequestLoanAbortDTO content = new RequestLoanAbortDTO();
		content.setLoanNumber(loanNumber);
		content.setExitTime(DateTimeUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
		JieYueResponseData response = jieYueSend.abortLoan(cooperorgCode, content);
		if(response!=null){
			if(com.jy.modules.cims.common.open.api.enums.EntryResponseEnum.SUCESS21001.getCode().equals(response.getRetCode())){
				JieYueMessage result = (JieYueMessage)response.getData();
				if(CimsConstant.JIEYU_RET_CODE_SUC.equals(result.getResult())){
					logger.info("合作机构【{}】进件终止接口调用成功！合同号【{}】！",cooperorgCode,loanNumber);
				}else{
					logger.info("合作机构【{}】进件终止接口调用失败！合同号【{}】，异常信息【{}】！",cooperorgCode,loanNumber,result.getMessage());
					throw new Exception("合作机构【"+cooperorgCode+"】进件终止接口调用失败！合同号【"+loanNumber+"】，异常信息【"+result.getMessage()+"】！");
				}
			}else{
				logger.info("合作机构【{}】进件终止接口调用返回结果异常，异常编号【{}】，信息【{}】！",cooperorgCode,response.getRetCode(),response.getMessage());
				throw new Exception("合作机构【"+cooperorgCode+"】进件终止接口调用返回结果异常，异常编号【"+response.getRetCode()+"】，信息【"+response.getMessage()+"】！！");
			}
		}else{
			logger.info("合作机构【{}】进件终止接口调用返回结果为空！",cooperorgCode);
			throw new Exception("合作机构【"+cooperorgCode+"】进件终止接口调用返回结果为空！");
		}
		logger.info("合作机构【{}】！紧急扣回结束>>",cooperorgCode);
	}
}
